package utils

import (
	"context"
	"github.com/redis/go-redis/v9"
	"log"
	"time"
)

var RedisClient *redis.Client

func init() {
	rdb := redis.NewClient(&redis.Options{
		Addr:     "192.168.64.5:6379",
		Password: "745305",
		DB:       0,
	})
	_, err := rdb.Ping(context.Background()).Result()
	if err != nil {
		log.Fatal("Redis连接失败，请检查")
	}
	RedisClient = rdb
}
func AddHash(key string, val ...string) error {
	ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
	defer cancel()
	return RedisClient.HSet(ctx, key, val).Err()
}

func AddStr(key string, val any) error {
	ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
	defer cancel()
	return RedisClient.GetSet(ctx, key, val).Err()
}
